package cc.wsyw126.java.nowcoder.sword_offer;


import java.util.HashMap;

/**
 * Created by Administrator on 2017/4/14.
 */

/**
 * 在一个字符串(1<=字符串长度<=10000，全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置。如果字符串为空,返回-1
 */
public class FirstNotRepeatingChar {
    public int FirstNotRepeatingChar(String str) {
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            Integer integer = characterIntegerHashMap.get(str.charAt(i));
            if (integer == null) {
                integer=0;
            }
            characterIntegerHashMap.put(str.charAt(i), ++integer);
        }
        for (int i = 0; i < str.length(); i++) {
            Integer integer = characterIntegerHashMap.get(str.charAt(i));
            if (integer == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        String s = "google";
        FirstNotRepeatingChar firstNotRepeatingChar = new FirstNotRepeatingChar();
        int i = firstNotRepeatingChar.FirstNotRepeatingChar(s);
        System.out.println("i = " + i);
    }
}
